What is browser-or-node?
The 'browser-or-node' npm package is a utility that helps developers determine whether their code is running in a browser or Node.js environment. This can be particularly useful for writing isomorphic JavaScript that needs to behave differently depending on the runtime environment.
What are browser-or-node's main functionalities?
Detect if running in a browser
This feature allows you to check if the code is being executed in a browser environment. The 'isBrowser' boolean will be true if the code is running in a browser.
const { isBrowser } = require('browser-or-node');
if (isBrowser) {
console.log('Running in a browser');
}
Detect if running in Node.js
This feature allows you to check if the code is being executed in a Node.js environment. The 'isNode' boolean will be true if the code is running in Node.js.
const { isNode } = require('browser-or-node');
if (isNode) {
console.log('Running in Node.js');
}
Detect if running in a web worker
This feature allows you to check if the code is being executed in a Web Worker environment. The 'isWebWorker' boolean will be true if the code is running in a Web Worker.
const { isWebWorker } = require('browser-or-node');
if (isWebWorker) {
console.log('Running in a Web Worker');
}
Other packages similar to browser-or-node
is-browser
The 'is-browser' package provides a simple way to check if the code is running in a browser environment. It is similar to 'browser-or-node' but does not offer the same level of granularity, such as detecting Node.js or Web Workers.
detect-node
The 'detect-node' package is a lightweight utility to check if the code is running in a Node.js environment. Unlike 'browser-or-node', it does not provide functionality to detect browser or Web Worker environments.
is-node
The 'is-node' package is another utility to determine if the code is running in a Node.js environment. It is similar to 'detect-node' and does not offer the ability to detect browser or Web Worker environments.
Browser or Node.js
Check whether the code is running in the browser or node.js runtime.
Install
$ npm install --save browser-or-node
Usage
ES6 style import
import { isBrowser, isNode, isWebWorker, isJsDom } from "browser-or-node";
if (isBrowser) {
}
if (isNode) {
}
if (isWebWorker) {
}
if (isJsDom) {
}
ES5 style import
var jsEnv = require("browser-or-node");
if (jsEnv.isBrowser) {
}
if (jsEnv.isNode) {
}
if (jsEnv.isWebWorker) {
}
if (jsEnv.isJsDom) {
}
License
MIT © Dineshkumar Pandiyan